package cc.autoapi.pucong.auto.flow.core.flow.execNode.exec.handler;

import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.api.ApiHandlerMsgDto;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.ExecNode;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.ExecParamOut;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.task.ApiExecResult;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.eventBus.ISubscriberAbstract;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.exec.DataBus;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.MsgTypeEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.RunStatusEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.utils.DataValueUtils;
import com.alibaba.fastjson2.JSONObject;

import java.util.List;

/**
 * api 处理器
 */
public class ProcessExecApiHandler extends ISubscriberAbstract<ApiHandlerMsgDto> {


    @Override
    public String getMessageType() {
        return MsgTypeEnum.API_MSG.getType();
    }

    @Override
    public Integer getIndexSort() {
        return 0;
    }

    @Override
    public void receiveMessage(ApiHandlerMsgDto message) {
        String instanceId = message.getInstanceId();
        RunStatusEnum runStatusEnum = message.getRunStatus();
        if (runStatusEnum != null) {
            if (runStatusEnum == RunStatusEnum.执行中) {
                ApiExecResult nodeExecResult = new ApiExecResult();
                nodeExecResult.setRunStatus(runStatusEnum);
                DataBus.setApiStatus(instanceId, nodeExecResult);
            } else if (runStatusEnum == RunStatusEnum.执行成功) {
                // 获取数据
                JSONObject runResult = getRunResult(instanceId);
                ApiExecResult nodeExecResult = new ApiExecResult();
                nodeExecResult.setRunStatus(runStatusEnum);
                nodeExecResult.setData(runResult);
                DataBus.setApiStatus(instanceId, nodeExecResult);
            } else if (runStatusEnum == RunStatusEnum.执行失败) {
                ApiExecResult nodeExecResult = new ApiExecResult();
                nodeExecResult.setRunStatus(runStatusEnum);
                nodeExecResult.setErrorLog(message.getLogContent());
                DataBus.setApiStatus(instanceId, nodeExecResult);
            }
        }
    }

    private JSONObject getRunResult(String instanceId) {
        JSONObject jsonObject = new JSONObject();
        List<ExecNode> execNodeList = DataBus.getExecNodeList(instanceId);
        for (ExecNode execNode : execNodeList) {
            if (execNode.getNodeExecResult().getRunStatus() != RunStatusEnum.执行成功) {
                continue;
            }
            List<ExecParamOut> execParamOutList = execNode.getParamOutList();
            for (ExecParamOut execParamOut : execParamOutList) {
                Boolean returnValue = execParamOut.getReturnValue();
                if (returnValue) {
                    jsonObject.put(execParamOut.getParamCode(), DataValueUtils.transDataType(execParamOut.getParamCode(), execParamOut.getDateType(), execParamOut.getDateValue()));
                }
            }
        }
        return jsonObject;
    }
}
